<?php
session_start ();
session_register ( "phieuDatHangChuaXuLy" );

class PhieuDatHangController extends Zend_Controller_Action {
	
	public function init() {
		/* Initialize action controller here */
	}
	
	public function indexAction() {
		// action body
	}
	
	public function getallphieudathangAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$phieuDatHang = $phieuDatHangService->getAllPhieuDatHang ();
		$this->view->phieuDatHang = $phieuDatHang;
		//phần tìm kiếm	
		$noiDungTimKiem = $_POST ['noiDungTimKiem'];
		$tieuChiTimKiem = $_POST ['tieuChiTimKiem'];
		if ($noiDungTimKiem != '' && $tieuChiTimKiem != '') {
			$ketQuaTimKiem = 'ketqua';
			$this->view->ketQuaTimKiem = $ketQuaTimKiem;
			$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
			$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
			$phieuDatHangTimKiem = $phieuDatHangService->timKiemPhieuDatHang ( $noiDungTimKiem, $tieuChiTimKiem );
			$this->view->phieuDatHangTimKiem = $phieuDatHangTimKiem;
		}
	}
	
	public function chitietphieudathangAction() {
		$idPhieuDatHang = $_GET ['idPhieuDatHang'];
		if($idPhieuDatHang==''){
			$idPhieuDatHang=$_POST['idPhieuDatHang'];
		}
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$chiTietPhieuDatHangService = Application_Model_service_ChiTietPhietDatHangService::getInstance ();
		$khachHangService = Application_Model_service_KhachHangService::getInstance ();
		$khachHangService instanceof Application_Model_service_KhachHangService;
		$TaiKhoanService = Application_Model_service_TaiKhoanService::getInstance ();
		try {
			$phieuDatHang = $phieuDatHangService->getPhieuDatHang ( $idPhieuDatHang );
			$this->view->phieuDatHang = $phieuDatHang;
			$maTaiKhoan = $phieuDatHang->getMaTaiKhoan (); //lay ma tai khoan tu phieu dat hang
			$maKhachHang = $phieuDatHang->getMaKhachHang ();
			//			$idTaiKhoan = $_SESSION ['id'] = 1;
			$taiKhoan = $TaiKhoanService->timkiemTaiKhoan ( $maTaiKhoan );
			$khachHang = $khachHangService->getKhachHang ( $maKhachHang );
			$this->view->taiKhoan = $taiKhoan;
			$this->view->khachHang = $khachHang;
			//lay tat ca cac chitietphieudathang cua mot phieu hang
			$chiTietPhieuDatHang = $chiTietPhieuDatHangService->getAllChiTietPhieuDatHang ( $idPhieuDatHang );
			$this->view->chiTietPhieuDatHang = $chiTietPhieuDatHang;
		
		} catch ( Zend_Exception $e ) {
			echo $e->getMessage ();
		}
	}
	
	public function getallphieudathangUserAction() {
		$idTaiKhoan = $_SESSION ['id'];
		if ($idTaiKhoan != '') {
			$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
			$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
			$phieuDatHang = $phieuDatHangService->getAllPhieuHang ( $idTaiKhoan );
			$this->view->phieuDatHang = $phieuDatHang;
		} else {
			echo 'chưa đăng nhập';
		}
	}
	
	public function chitietphieudathangUserAction() {
		$idPhieuDatHang = $_POST ['idPhieuDatHang'];
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$chiTietPhieuDatHangService = Application_Model_service_ChiTietPhietDatHangService::getInstance ();
		$TaiKhoanService = Application_Model_service_TaiKhoanService::getInstance ();
		try {
			$phieuDatHang = $phieuDatHangService->getPhieuDatHang ( $idPhieuDatHang );
			$this->view->phieuDatHang = $phieuDatHang;
			$maTaiKhoan = $phieuDatHang->getMaTaiKhoan (); //lay ma tai khoan tu phieu dat hang
			$idTaiKhoan = $_SESSION ['id'];
			$taiKhoan = $TaiKhoanService->timkiemTaiKhoan ( $idTaiKhoan );
			$this->view->taiKhoan = $taiKhoan;
			//lay tat ca cac chitietphieudathang cua mot phieu hang
			$chiTietPhieuDatHang = $chiTietPhieuDatHangService->getAllChiTietPhieuDatHang ( $idPhieuDatHang );
			$this->view->chiTietPhieuDatHang = $chiTietPhieuDatHang;
		
		} catch ( Zend_Exception $e ) {
			echo $e->getMessage ();
		}
	}
	
	public function capnhattrangthaiphieudathangAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$idPhieuDatHangCapNhat = $_POST ['idPhieuDatHangCapNhat'];
		if ($idPhieuDatHangCapNhat != '') {
			$trangThai = true;
			$phieuDatHangService->capNhatTrangThaiPhieuDatHang ( $idPhieuDatHangCapNhat, $trangThai );
			$this->_helper->layout ()->disableLayout ();
		}
	}
	
	public function huyphieudathangAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$idPhieuDatHangHuy = $_POST ['idPhieuDatHangHuy'];
		if ($idPhieuDatHangHuy != '') {
			$phieuDatHangService->huyPhieuDatHang ( $idPhieuDatHangHuy );
			$this->_helper->layout ()->disableLayout ();
		}
	}
	
	public function getallphieudathanghuyAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$idPhieuDatHangPhucHoi = $_POST ['idPhieuDatHangPhucHoi'];
		if ($idPhieuDatHangPhucHoi != '') {
			$phieuDatHangService->phucHoiPhieuDatHang ( $idPhieuDatHangPhucHoi );
			$this->_helper->layout ()->disableLayout ();
		}
		$phieuDatHang = $phieuDatHangService->getAllPhieuDatHangHuy ();
		$this->view->phieuDatHang = $phieuDatHang;
		
	//phần tìm kiếm
		$noiDungTimKiem = $_POST ['noiDungTimKiem'];
		$tieuChiTimKiem = $_POST ['tieuChiTimKiem'];
		if ($noiDungTimKiem != '' && $tieuChiTimKiem != '') {
			$ketQuaTimKiem = 'ketqua';
			$this->view->ketQuaTimKiem = $ketQuaTimKiem;
			$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
			$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
			$phieuDatHangTimKiem = $phieuDatHangService->timKiemPhieuDatHang ( $noiDungTimKiem, $tieuChiTimKiem );
			$this->view->phieuDatHangTimKiem = $phieuDatHangTimKiem;
		}
	}
	
	public function phuchoiphieudathangAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$idPhieuDatHangPhucHoi = $_POST ['idPhieuDatHangPhucHoi'];
		if ($idPhieuDatHangPhucHoi != '') {
			$phieuDatHangService->phucHoiPhieuDatHang ( $idPhieuDatHangPhucHoi );
			$this->_helper->layout ()->disableLayout ();
		}
	}
	
	public function getallphieudathangdaxulyAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$phieuDatHang = $phieuDatHangService->getAllPhieuDatHangDaXuLy ();
		$this->view->phieuDatHang = $phieuDatHang;
		
		//phần tìm kiếm
		$noiDungTimKiem = $_POST ['noiDungTimKiem'];
		$tieuChiTimKiem = $_POST ['tieuChiTimKiem'];
		if ($noiDungTimKiem != '' && $tieuChiTimKiem != '') {
			$ketQuaTimKiem = 'ketqua';
			$this->view->ketQuaTimKiem = $ketQuaTimKiem;
			$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
			$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
			$phieuDatHangTimKiem = $phieuDatHangService->timKiemPhieuDatHang ( $noiDungTimKiem, $tieuChiTimKiem );
			$this->view->phieuDatHangTimKiem = $phieuDatHangTimKiem;
		}
	}
	
	public function getallphieudathangchuaxulyAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$idPhieuDatHangCapNhat = $_POST ['idPhieuDatHangCapNhat'];
		if($idPhieuDatHangCapNhat==''){
			$idPhieuDatHangCapNhat= $_GET ['idPhieuDatHangCapNhat'];
		}
		$idPhieuDatHangHuy = $_POST ['idPhieuDatHangHuy'];
		if($idPhieuDatHangHuy==''){
			$idPhieuDatHangHuy = $_GET ['idPhieuDatHangHuy'];
		}
		if ($idPhieuDatHangCapNhat != '') {
			$trangThai = true;
			$phieuDatHangService->capNhatTrangThaiPhieuDatHang ( $idPhieuDatHangCapNhat, $trangThai );
			$this->_helper->layout ()->disableLayout ();
		}
		if ($idPhieuDatHangHuy != '') {
			$phieuDatHangService->huyPhieuDatHang ( $idPhieuDatHangHuy );
			$this->_helper->layout ()->disableLayout ();
		}
		$phieuDatHang = $phieuDatHangService->getAllPhieuDatHangChuaXuLy ();
		$this->view->phieuDatHang = $phieuDatHang;
		$_SESSION ['phieuDatHangChuaXuLy'] = $phieuDatHang;
		
		//phần tìm kiếm	
		$noiDungTimKiem = $_POST ['noiDungTimKiem'];
		$tieuChiTimKiem = $_POST ['tieuChiTimKiem'];
		if ($noiDungTimKiem != '' && $tieuChiTimKiem != '') {
			$ketQuaTimKiem = 'ketqua';
			$this->view->ketQuaTimKiem = $ketQuaTimKiem;
			$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
			$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
			$phieuDatHangTimKiem = $phieuDatHangService->timKiemPhieuDatHang ( $noiDungTimKiem, $tieuChiTimKiem );
			$this->view->phieuDatHangTimKiem = $phieuDatHangTimKiem;
		}
	}
	
	public function reportphieudathangchuaxulyAction() {
		$phieuDatHangService = Application_Model_service_PhieuDatHangService::getInstance ();
		$phieuDatHangService instanceof Application_Model_service_PhieuDatHangService;
		$tenFile = $_POST ['tenFile'];
		$report = $_POST ['report'];
		$phieuDatHang = $_SESSION ['phieuDatHangChuaXuLy'];

		if ($tenFile != '' && $report != '') {
			$argv [1] = $tenFile . '.pdf';
			try {
				$pdf = Zend_Pdf::load ( $argv [1] );
			} catch ( Zend_Pdf_Exception $e ) {
				if ($e->getMessage () == 'Can not open \'' . $argv [1] . '\' file for reading.') {
					//tạo file mới
					$pdf = new Zend_Pdf ();
					if (! isset ( $argv [2] )) {
						// update
						$argv [2] = $argv [1];
					}
				} else {
					throw $e;
				}
			}
			
			try {
				// Reverse page order
				$pdf->pages = array_reverse ( $pdf->pages );
				
				// Create new Style
				$style = new Zend_Pdf_Style ();
				$style->setFillColor ( new Zend_Pdf_Color_Rgb ( 0, 0, 0.9 ) );
				$style->setLineColor ( new Zend_Pdf_Color_GrayScale ( 0.2 ) );
				$style->setLineWidth ( 3 );
				$style->setLineDashingPattern ( array (3, 2, 3, 4 ), 1.6 );
				$style->setFont ( Zend_Pdf_Font::fontWithName ( Zend_Pdf_Font::FONT_HELVETICA_BOLD ), 32 );
				try {
					// Create new image object
				//    require_once 'Zend/Pdf/Image.php';
				//    $stampImage = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/stamp.jpg');
				} catch ( Zend_Pdf_Exception $e ) {
					// Example of operating with image loading exceptions.
					if ($e->getMessage () != 'Image extension is not installed.' && $e->getMessage () != 'JPG support is not configured properly.') {
						throw $e;
					}
					//    $stampImage = null;
				}
				// Mark page as modified
				foreach ( $pdf->pages as $page ) {
					$page->saveGS ()->setAlpha ( 0.25 )->setStyle ( $style )->rotate ( 0, 0, M_PI_2 / 3 );
					
					$page->saveGS ();
					$page->clipCircle ( 550, - 10, 50 );
					//    if ($stampImage != null) {
					//        $page->drawImage($stampImage, 500, -60, 600, 40);
					//    }
					$page->restoreGS ();
					
					$page->drawText ( 'Modified by Zend Framework!', 150, 0 )->restoreGS ();
				}
				
				// Add new page generated by Zend_Pdf object (page is attached to the specified the document)
				$pdf->pages [] = ($page1 = $pdf->newPage ( 'A4' ));
				
				// Add new page generated by Zend_Pdf_Page object (page is not attached to the document)
				//$pdf->pages[] = ($page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE));
				

				// Tạo font mới
				$font = Zend_Pdf_Font::fontWithName ( Zend_Pdf_Font::FONT_HELVETICA );
				
				// Sử dụng font và màu
				$page1->setFont ( $font, 30 )->setFillColor ( Zend_Pdf_Color_Html::color ( '#9999cc' ) )->drawText ( 'Report', 10, 750 );
				
				//nhap de muc
				$mang = array ('Ma Phieu Dat Hang', 'Gia Tri', 'Ngay Tao', 'Trang Thai' );
				$m = 0;
				for($i = 50; $i <= 500; $i += 125) {
					$j = $i + 125;
					$k = $i + 5;
					$page1->setFillColor ( new Zend_Pdf_Color_GrayScale ( 1 ) )->setLineColor ( Zend_Pdf_Color_Html::color ( '#000000' ) )->drawRectangle ( $i, 700, $j, 670 );
					$page1->setFont ( $font, 10 )->setFillColor ( Zend_Pdf_Color_Html::color ( '#9999cc' ) )->drawText ( $mang [$m], $k, 685 );
					$m ++;
				}
				
				//nhap du lieu
				if (count ( $phieuDatHang ) > 0) {
					$x = 700 - 30;
					$y = 670 - 30;
					for($h = 0; $h < count ( $phieuDatHang ); $h ++) {
						$phieuDH = $phieuDatHang [$h];
						$phieuDH instanceof Application_Model_bean_PhieuDatHang;
						$maPDH = $phieuDH->getMaPhieuDatHang ();
						$giaTri = $phieuDH->getGiaTri ();
						$ngayTao = $phieuDH->getNgayTao ();
						$mangDuLieu = array ($maPDH, $giaTri, $ngayTao, 'Chua Xu Ly' );
						$m = 0;
						for($i = 50; $i <= 500; $i += 125) {
							$j = $i + 125;
							$k = $i + 5;
							$page1->setFillColor ( new Zend_Pdf_Color_GrayScale ( 1 ) )->setLineColor ( Zend_Pdf_Color_Html::color ( '#000000' ) )->drawRectangle ( $i, $x, $j, $y );
							$page1->setFont ( $font, 10 )->setFillColor ( Zend_Pdf_Color_Html::color ( '#9999cc' ) )->drawText ( $mangDuLieu [$m], $k, $x - 15 );
							$m ++;
						}
						$x -= 30;
						$y -= 30;
					}
				}
				
				if (isset ( $argv [2] )) {
					$pdf->save ( $argv [2] );
				} else {
					$pdf->save ( $argv [1], true /* update */);
				}
				$duongDanFileReport = '../'.$tenFile . '.pdf';
				$this->view->duongDanFileReport = $duongDanFileReport;
			} catch ( Zend_Exception $e ) {
			
			}
		
		} else if ($tenFile == '' && $report != '') {
			echo 'ten file rong';
		}
	}
}





























